package delivery.order.service.utils;

import delivery.order.service.constant.PathConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FolderUtil {

    private static final Logger logger = LoggerFactory.getLogger(FolderUtil.class);

    /**
     * 检查是否有输出文件夹，没有就创建
     */
    public static void checkAndCreateOutputFolder() {
        // 指定D盘根目录下的output文件夹路径
        File folder = new File(PathConstant.getOutputFolderPath());
        // 检查文件夹是否存在
        if (!folder.exists()) {
            // 尝试创建文件夹
            boolean created = folder.mkdir();
            if (created) {
                logger.warn("output文件夹创建成功: {}", PathConstant.getOutputFolderPath());
            } else {
                logger.error("output文件夹创建失败: {}", PathConstant.getOutputFolderPath());
            }
        }
    }

    /**
     * 删除指定路径的文件
     */
    public static void deleteFile(String filePath) {
        Path path = Paths.get(filePath);
        try {
            if (Files.exists(path)) {
                Files.delete(path);
            }
        } catch (IOException e) {
            logger.error("删除文件时出错: {}", e.getMessage());
        }
    }

}